/**
 * @summary 文章请求模块
 */

import req from '@/utils/request';
import * as URL from '@/constants/api';

/**
 * @param { string } id
 * @returns
 * @summary 获取文章列表
 */
export function getArticleList(id) {
  return req({
    url: URL.ARTICLE,
    params: {
      channel_id: id,
      timestamp: Date.now(),
    },
  })
    .then((res) => res.data.data.results)
    .catch(() => []);
}

/**
 * @param { string } id
 * @returns
 * @summary 获取文章详情
 */
export function getArticleDetail(id) {
  return req({
    url: `${ URL.ARTICLE }/${ id }`,
  })
    .then((res) => res.data.data) // to do
    .catch(() => []);
}

// 点赞
export function articleLiking(id) {
  return req({
    url: URL.ARTICLE_LIKINGS,
    method: 'POST',
    data: {
      target: id
    }
  })
    .then(res => res.data.message)
    .catch(err => Promise.reject(err.response.data.message));
}

// 取消点赞
export function disArticleLiking(id) {
  return req({
    url: `${URL.ARTICLE_LIKINGS}/${id}`,
    method: 'DELETE',
  })
    .then(res => res.data?.message || '取消成功')
    .catch(err => Promise.reject(err.response.data?.message || '取消失败'));
}

// 收藏
export function collectArticle(id) {
  return req({
    url: URL.ARTICLE_LIKINGS,
    method: 'POST',
    data: {
      target: id
    }
  })
    .then(res => res.data.message)
    .catch(err => Promise.reject(err.response.data.message));
}

// 取消收藏
export function discollectArticle(id) {
  return req({
    url: `${URL.ARTICLE_COLLECT}/${id}`,
    method: 'DELETE',
  })
    .then(res => res.data?.message || '取消成功')
    .catch(err => Promise.reject(err.response.data?.message || '取消失败'));
}
